package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.SoulOptions;
import com.itheima.domain.db.SoulPaper;
import com.itheima.domain.db.SoulQuestion;
import com.itheima.domain.vo.SoulQuestionVo2;
import com.itheima.service.db.TestSoulService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SoulTestAdminManager {
    @Reference
    private TestSoulService testSoulService;
    //查询所有试题列表
    public List<SoulPaper> findTestSoulList() {
        List<SoulPaper> soulTestList = testSoulService.findSoulTestList();
        return soulTestList;
    }

    //根据试题id查询试题详细信息
    public List<SoulQuestionVo2> findSoulQuestionListBySid(String sid) {
        List<SoulQuestion> soulQuestionList = testSoulService.findSoulQuestionBySid(sid);
        List<SoulQuestionVo2> soulQuestionVoList =new ArrayList<>();
        if (CollectionUtil.isNotEmpty(soulQuestionList)){
            for (SoulQuestion soulQuestion : soulQuestionList) {
                SoulQuestionVo2 soulQuestionVo =new SoulQuestionVo2();
                Long qid = soulQuestion.getId();
                List<SoulOptions> optionList = testSoulService.findOptionByQid(qid);
                soulQuestionVo.setOptions(optionList);
                BeanUtil.copyProperties(soulQuestion, soulQuestionVo);
                soulQuestionVoList.add(soulQuestionVo);
            }
        }
        return soulQuestionVoList;
    }

    public SoulQuestionVo2 findSoulQuestionListById(String id) {
        SoulQuestion soulQuestion = testSoulService.findSoulQuestionById(id);
        List<SoulOptions> optionList = testSoulService.findOptionByQid(Long.valueOf(id));
        SoulQuestionVo2 soulQuestionVo = new SoulQuestionVo2();
        BeanUtil.copyProperties(soulQuestion, soulQuestionVo);
        soulQuestionVo.setOptions(optionList);
        return soulQuestionVo;
    }



    //根据问题id删除问题
    public String deleteSoulQuestionById(String soulQuestionId) {
        return testSoulService.deleteSoulQuestionById(soulQuestionId);
    }

    //根据试卷id添加问题
    public String addSoulQuestion(String soulTestId, SoulQuestionVo2 soulQuestionVo) {
        //保存问题
        System.out.println(soulTestId);
        SoulQuestion soulQuestion = new SoulQuestion();
        soulQuestion.setType(soulTestId);
        soulQuestion.setQuestion(soulQuestionVo.getQuestion());
        testSoulService.saveSoulQuestion(soulQuestion);
        Integer soulQuestionId = testSoulService.findSoulQuestionId();
        System.out.println(soulQuestionId);
        //保存选项
        List<SoulOptions> options = soulQuestionVo.getOptions();
        if (CollectionUtil.isNotEmpty(options)){
            for (SoulOptions option : options) {
                option.setQuestionId(Long.valueOf(soulQuestionId));
                option.setOptions(option.getOptions());
                option.setScore(option.getScore());
                testSoulService.saveOption(option);
            }
        }
        return "保存成功";
    }

    public Long updateSoulQuestionById(SoulQuestionVo2 soulQuestionVo) {
        //修改问题
        SoulQuestion soulQuestion = new SoulQuestion();
        //BeanUtil.copyProperties(soulQuestionVo,soulQuestion);
        soulQuestion.setId(Long.valueOf(soulQuestionVo.getId()));
        soulQuestion.setQuestion(soulQuestionVo.getQuestion());
        SoulQuestion soulQuestionById = testSoulService.findSoulQuestionById(soulQuestionVo.getId());
        soulQuestion.setId(soulQuestionById.getId());
        testSoulService.updateSoulQuestionById(soulQuestion);
        //修改选项
        List<SoulOptions> options = (List<SoulOptions>) soulQuestionVo.getOptions();
        if (CollectionUtil.isNotEmpty(options)){
            for (SoulOptions option : options) {
                option.setQuestionId(Long.valueOf(soulQuestionVo.getId()));
                testSoulService.updateOptionById(option);
            }
        }
        Long id = soulQuestionById.getId();
        return id;
    }
}